package com.sgb.kjwl.view.ui.activity.transport.company;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddProprietaryFleetBinding;
import com.sgb.kjwl.model.entity.transport.SelfFleetEntity;
import com.swgk.core.event.MenuEvent;
import com.sgb.kjwl.viewmodel.transport.AddProprietaryFleetViewModel;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

/**
 * author:熊丽
 * createDate:2021/10/9 15:31
 * introduce:新增自营车队
 */
public class AddProprietaryFleetActivity extends AppActivity {
    ActivityAddProprietaryFleetBinding mbinding;
    AddProprietaryFleetViewModel mfleetViewModel;
    private SelfFleetEntity mselfFleetEntity;
    private boolean isBack;

    public static void start(Context context, SelfFleetEntity mSelfEn) {
        Intent intent = new Intent(context, AddProprietaryFleetActivity.class);
        intent.putExtra("selfFleetEntity", mSelfEn);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    /**
     * 添加自营司机
     * 此跳转方式，成功添加司机后,需要根据业务逻辑判断是否回到来源页，进行后续业务
     *
     * @param context
     */
    public static void start(Context context, boolean isBack) {
        Intent intent = new Intent(context, AddProprietaryFleetActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("isBack", isBack);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        mbinding = DataBindingUtil.setContentView(this, R.layout.activity_add_proprietary_fleet);
        mselfFleetEntity = (SelfFleetEntity) getIntent().getSerializableExtra("selfFleetEntity");
        isBack = getIntent().getBooleanExtra("isBack",false);
        mfleetViewModel = new ViewModelProvider(this).get(AddProprietaryFleetViewModel.class);
        mbinding.includeTitle.titleView.setText(mselfFleetEntity == null ? "新增自营车队" : "编辑自营车队");
        mbinding.includeTitle.ivBack.setOnClickListener(this::onMultiClick);
        mbinding.tvAddFleetSubmit.setOnClickListener(this::onMultiClick);
        if (mselfFleetEntity != null) {
            edit(mselfFleetEntity);
        }
        initData();
    }

    private void initData() {
        mfleetViewModel.mlAddFleet.observe(this, sucess -> {
            if (TextUtils.isEmpty(sucess)) {
                ToastUtil.show(this, "接口有误");
                return;
            } else if (isBack) {
                EventBus.getDefault().postSticky(MenuEvent.make(EventConstant.EVENT_REFRESH_PRO_DRIVER, null));
                finish();
            } else {
                finish();
                if (mselfFleetEntity != null && !TextUtils.isEmpty(mselfFleetEntity.getMotorcadeNo())) {
                    AffiliatedFleetDetailsActivity.start(this, true, mselfFleetEntity.getMotorcadeNo());
                } else {
                    AffiliatedFleetDetailsActivity.start(this, true, sucess);
                }

            }
        });
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_fleet_submit:
                submit();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void submit() {
        if (TextUtils.isEmpty(mbinding.etInputCarName.getText())) {
            MToast.showToast(this, "请输入车队名称");
            return;
        }
        if (TextUtils.isEmpty(mbinding.etAddContactsName.getText())) {
            MToast.showToast(this, "请输入联系人");
            return;
        }
        if (TextUtils.isEmpty(mbinding.etAddPhone.getText())) {
            MToast.showToast(this, "请输入手机号");
            return;
        }
        /***
         * 验证手机号码格式是否正确
         */
        if (!CheckUtils.isMobileNum(mbinding.etAddPhone.getText().toString())) {
            MToast.showToast(this, "请输入正确手机格式");
            return;
        }
        mfleetViewModel.submitData().setMotorcadeName(mbinding.etInputCarName.getText().toString());
        mfleetViewModel.submitData().setName(mbinding.etAddContactsName.getText().toString());
        mfleetViewModel.submitData().setTelphone(mbinding.etAddPhone.getText().toString());
        mfleetViewModel.submitData().setMotorcadeNo(mselfFleetEntity == null || TextUtils.isEmpty(mselfFleetEntity.getMotorcadeNo()) ? "" : mselfFleetEntity.getMotorcadeNo());
        if (mselfFleetEntity != null && mselfFleetEntity.getId() != 0) {
            mfleetViewModel.submitData().setId(mselfFleetEntity.getId());
        }
        mfleetViewModel.setAddfleet(this);
    }

    private void edit(SelfFleetEntity mselfFleetEntity) {
        mbinding.etAddContactsName.setText(mselfFleetEntity.getName());
        mbinding.etAddPhone.setText(mselfFleetEntity.getTelphone());
        mbinding.etInputCarName.setText(mselfFleetEntity.getMotorcadeName());
    }
}
